package org.xshop.web;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.xshop.model.TGoods;
import org.xshop.model.TGoodsClassify;
import org.xshop.model.THotSearch;
import org.xshop.service.ITGoodsClassifyService;
import org.xshop.service.ITGoodsService;
import org.xshop.service.ITHotSearchService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2017-03-19
 */
@Controller
@RequestMapping("/app/goods/")
@Api(tags = "Goods", value = "APP商品接口", description = "APP-商品接口")
public class TGoodsController extends AppBaseController<TGoods, ITGoodsService> {
    @Autowired
    private ITGoodsClassifyService goodsClassifyService;
    @Autowired
    private ITHotSearchService hotSearchService;
    private static ExecutorService executorService = Executors.newCachedThreadPool();

    @ApiOperation(value = "查询商品分类", produces = MediaType.APPLICATION_JSON_VALUE, notes = "获取商品分类列表", response = TGoodsClassify.class)
    @RequestMapping(value = "/classify.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object getAllClassify(HttpServletRequest request) {
        Map<String, Object> param = InstanceUtil.newHashMap();
        List<?> result = goodsClassifyService.getAll(param);
        return setSuccessModelMap(result);
    }

    @ApiOperation(value = "查询商品", produces = MediaType.APPLICATION_JSON_VALUE, response = TGoods.class, notes = "按指定的条件查询商品列表")
    @RequestMapping(value = "/query.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object getAllGoods(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        if (DataUtil.isNotEmpty(param.get("keyword"))) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    THotSearch record = new THotSearch();
                    record.setKey(param.get("keyword").toString());
                    hotSearchService.update(record);
                }
            });
        }
        param.put("isSale", "1");
        param.put("enable", "1");
        ModelMap modelMap = new ModelMap();
        modelMap.put("rows", service.getAllGoods(param));
        return setSuccessModelMap(modelMap);
    }

    @RequestMapping(value = "/detail.api", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", produces = MediaType.APPLICATION_JSON_VALUE, response = TGoods.class)
    public ResponseEntity<ModelMap> detail(HttpServletRequest request,
        @RequestParam @ApiParam(required = true, value = "商品ID") Long id) {
        Map<String, Object> param = WebUtil.getParameter(request);
        Long memberId = getCurrUser(request);
        if (DataUtil.isNotEmpty(memberId)) {
            param.put("memberId", memberId);
        }
        TGoods goods = service.getGoodsDetail(param);
        return setSuccessModelMap(goods);
    }

    @ApiOperation(value = "查询商店商品", produces = MediaType.APPLICATION_JSON_VALUE, response = TGoods.class, notes = "按指定的条件查询商品列表")
    @RequestMapping(value = "/getStoreGoods.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object getStoreGoods(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        param.put("isSale", "1");
        param.put("enable", "1");
        Object result = service.getStoreGoods(param);
        return setSuccessModelMap(result);
    }
}
